<?php

namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Requests\Persional;
use App\Models\Activity;
use App\Models\ActivityGather;
use App\Models\ActivityImgs;
use App\Models\Banner;
use App\Models\Classify;
use App\Models\Introduce;
use App\Models\Personal as PersonalModel;
use Illuminate\Http\Request;

class ActivityController extends Controller {

    public function banner(Request $request){
        $bannerModel = new Banner();
        $list = $bannerModel->getData();

        foreach ($list['list'] as $l){
            $l->img = env('APP_URL'). $l->img;
        }

        return response()->json(['code' => 0, 'msg' => '', 'data' => $list]);
    }

    public function activity(Request $request){
        $page = $request->get('page', 1);

        $search = [];
        if($request->has('searchKey') && $request->get('searchKey') != 'null'){
            $search['title'] = $request->get('searchKey');
        }
        if($request->has('cid') && $request->get('cid') != 'null' && $request->get('cid') != 0){
            $search['cid'] = $request->get('cid');
        }
        $activityModel = new Activity();
        $list = $activityModel->getData($search, $page, 20);

        foreach ($list['list'] as $l){
            $l->off_line_time = date('m-d', strtotime($l->off_line_time));
        }


        foreach ($list['list'] as $l){
            $l->img = env('APP_URL'). $l->img;
        }

        return response()->json(['code' => 0, 'msg' => '', 'data' => $list]);
    }

    public function activityInfo(Request $request) {
        $id = $request->get('id');
        $activityModel = new Activity();
        $list = $activityModel->getData(['id' => $id]);
        foreach ($list['list'] as $l){
            $l->img = env('APP_URL'). $l->img;
        }

        $imgs = ActivityImgs::where('activity_id', $id)->whereNull('deleted_at')->get();
        foreach ($imgs as $i){
            $i->img = env('APP_URL'). $i->img;
        }
        if(isset($list['list'][0])){
            $list = $list['list'][0];
            $list->imgs = $imgs;
        }
        return response()->json(['code' => 0, 'msg' => '', 'data' => $list]);
    }

    public function addLxr(Persional $request){
        global $g_uid;
        $uid = $g_uid;
        $data = $request->all();
        unset($data['token']);
        $data['u_id'] = $uid;
        $data['created_at'] = date('Y-m-d H:i:s', time());

        $personalModel = new PersonalModel();

        if($personalModel->saveData($data)){
            return response()->json(['code' => 0,'msg' => '添加成功', 'data' => []]);
        }else{
            return response()->json(['code' => -1,'msg' => '添加失败', 'data' => []]);
        }
    }

    public function updateLxr(Persional $request){
        if(!$request->has('id')){
            return response()->json(['code' => -1, 'msg' => '缺少要删除的联系人ID']);
        }
        $id = $request->get('id');
        $data = $request->all();
        unset($data['token']);
        unset($data['id']);

        if(PersonalModel::where('id', $id)->update($data) !== false ){
            return response()->json(['code' => 0,'msg' => '修改成功', 'data' => []]);
        }else{
            return response()->json(['code' => -1,'msg' => '修改失败', 'data' => []]);
        }
    }

    public function deleteLxr(Request $request){
        if(!$request->has('personal_id')){
            return response()->json(['code' => -1, 'msg' => '缺少要删除的联系人ID']);
        }
        global $g_uid;
        $uid = $g_uid;
        $personalId = $request->get('personal_id');

        if(PersonalModel::where('u_id', $uid)->where('id', $personalId)->update([
            'deleted_at' => date('Y-m-d H:i:s', time())
        ]) === false){
            return response()->json(['code' => -2, 'msg' => '删除失败']);
        }else{
            return response()->json(['code' => 0, 'msg' => '删除成功']);
        }
    }

    public function lxrInfo(Request $request){
        global $g_uid;
        $uid = $g_uid;
        $personalModel = new PersonalModel();
        $list = $personalModel->getData(['u_id' => $uid, 'id' => $request->get('id')]);

        $list = isset($list['list'][0])?$list['list'][0]:[];

        return response()->json(['code' => 0, 'msg' => '请求成功', 'data' => $list]);
    }

    public function lxr(){
        global $g_uid;
        $uid = $g_uid;
        $personalModel = new PersonalModel();
        $list = $personalModel->getPersonalData(['u_id' => $uid]);

        return response()->json(['code' => 0, 'msg' => '请求成功', 'data' => $list]);
    }

    public function enroll(Request $request){
        global $g_uid;
        $uid = $g_uid;
        if(!$request->has('activity_id')){
            return response()->json(['code' => -1, 'msg' => '缺少活动id']);
        }
        if(!$request->has('personal_ids')){
            return response()->json(['code' => -2, 'msg' => '缺少联系人']);
        }

        $activityId = $request->get('activity_id');
        $personalIds = explode(',',$request->get('personal_ids'));
        $hasChild = $request->get('has_child');
        $remark = $request->get('remark');

        $enrollData = [
            'activity_id' => $activityId,
            'has_child' => $hasChild,
            'remark' => $remark,
            'u_id' => $uid,
            'created_at' => date('Y-m-d H:i:s', time())
        ];
        $activityBlock = new \App\Blocks\Activity();
        if($activityBlock->enroll($enrollData, $personalIds)){
            return response()->json(['code' => 0, 'msg'=>'报名成功']);
        }else{
            return response()->json(['code' => -4, 'msg'=>'报名失败']);
        }
    }

    public function classify(){
        $classify = Classify::whereNull('deleted_at')->orderBy('sort', 'desc')->get();
        return response()->json(['code' => 0, 'msg' => '', 'data' => $classify]);
    }

    public function getIntroduce(){
        $introduce = Introduce::orderBy('create_at', 'desc')->first();

        return response()->json(['code' => 0, 'msg' => '请求成功', 'data' => $introduce]);
    }

    public function gather()
    {
        $activity_gather = new ActivityGather();
        $res = $activity_gather->gather_first();
        return response()->json(['code' => 0, 'msg' => '请求成功', 'data' => $res]);
    }
}